package com.gift.controller;

import com.gift.common.PaGing;
import com.gift.common.ServerResponse;
import com.gift.dao.BankCardMapper;
import com.gift.pojo.BankCard;
import com.gift.service.BankCardService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/7.
 */
@Controller
@RequestMapping("bankCard")
public class BankCardController {

    @Resource
    BankCardService bankCardService;
    @Resource
    BankCardMapper bankCardMapper;

    //跳转到银行卡信息首页
    @RequestMapping("/home")
    public String goHome(){
        return "BankCardController/bankCard";
    }


    //得到所有银行卡信息
    @RequestMapping("/getAll")
    @ResponseBody
    public PaGing getAll(int page, int rows){

        return bankCardService.getAllBypr(page,rows);
    }

    //跳转到新增银行卡页面
    @RequestMapping("/toInsetBankCard")
    public String toInsetBankCard(){
        return "BankCardController/newBankCard";
    }

    //保存新增银行卡信息
    @RequestMapping(value = "/insetBankCard" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse insetNewGame(BankCard newBankCard){
        return bankCardService.insetNewGame(newBankCard);
    }


    //跳转到修改银行卡页面
    @RequestMapping("/toupdataBankCard")
    public String toupdataBankCard(String bid, Model model){

        BankCard bankCard=bankCardService.getbankCardByBid(bid);

        model.addAttribute("bankCard",bankCard);
        return "BankCardController/newBankCard";
    }

    //修改银行卡信息
    @RequestMapping(value = "/updataBankCard",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updataGame(BankCard updataBankCard){
        return bankCardService.updataGame(updataBankCard);
    }

    //删除银行卡信息
    @RequestMapping(value = "deleteAllBankCard", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse deleteAll(@Param("deleteIdString") String deleteIdString ) {

        String[] strings= StringUtils.split(deleteIdString,",");
        ArrayList<Integer> deleteIdList = new ArrayList<>();
        for(int i = 0;i<strings.length;i++){
            deleteIdList.add(Integer.valueOf(strings[i]));
        }
        return  bankCardService.deleteGidList(deleteIdList);
    }


    //得到所有银行卡信息
    @RequestMapping("/getAllNo")
    @ResponseBody
    public List getAllNo(){

        return bankCardService.getAll();
    }

    //根据游戏得到对应的游戏账号列表
    @RequestMapping("/getAllByGid")
    @ResponseBody
    public List getAllByGid(@RequestParam("gid") String gid){

        return bankCardMapper.getAllByGid(Integer.valueOf(gid));
    }


}
